package com.woniuxy.hbum.user.dao;


import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.woniuxy.hbum.database.JDBConnection;
import com.woniuxy.hbum.entity.User;
import com.woniuxy.hbum.user.dto.UserDto;

public class UserDAO {

	public String getPassByName(String name) {
		String pass="";
		JDBConnection jdbc = new JDBConnection();
		Session session = jdbc.getSession();
		String hql = "from User as u where u.name=?";
		Query query = session.createQuery(hql);
		query.setString(0, name);
		List list = query.list();
		User user = (User) list.get(0);
		pass = user.getPassword();
		return pass;
	}

	public boolean insert(UserDto baseDTO) {
		boolean result=true;
		JDBConnection jdbc = new JDBConnection();
		Session session = jdbc.getSession();

		Transaction transaction = session.beginTransaction();
		try {	
		User user = new User();
		user.setName(baseDTO.getName());
		user.setPassword(baseDTO.getPassword());
		session.save(user);
		transaction.commit();
		} catch (Exception e) {
			transaction.rollback();
			result = false;
		}finally{
			jdbc.closeResource();
		}
		return result;
	}

}
